/* pages/car/car.js */
Page({
  data: {
    nickName:"",
    avatarUrl: './user-unlogin.png',
    userInfo: {},
    logged: false,
    takeSession: false,
    requestResult: '',
    // 车牌号
    arrProvience: ['京', '津', '冀', '晋', '蒙', '辽', '吉', '黑', '沪', '苏', '浙', '皖', '闽', '赣', '鲁', '豫', '鄂', '湘', '粤','桂', '琼', '渝', '川', '贵', '云', '藏', '陕', '甘', '青', '宁', '新'],
    arrNumber: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
    arrCaptainOne: ['Q', 'W', 'E', 'R', 'T', 'Y', 'U','I','O' ,'P'],
    arrCaptainTwo: ['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'],
    arrCaptainThree: ['Z', 'X', 'C', 'V', 'B', 'N', 'M'],
    arrlicheng: [1,2,3],
    lichengOne:1,
    activeIndex: 0,
    choosedArr:new Array(7),
    carView:true,
    lichengView: false,
    isLicheng: false,
    wanUnitIndex: 0,
    qianUnitIndex: 0
  },
  onLoad: function(e) {
    const token = wx.getStorageSync("token");
    var that = this;
    if (!token) {
    console.log("token不存在");
    wx.reLaunch({
      url: '/pages/auth/auth',
     })
     wx.showToast({
       title: '请先登陆授权',
     })
     return;
   }else{
    console.log("如果token存在");
    wx.request({
      url: 'http://localhost:8080/ipark/user/login',
      data: {
        token: token
      },
      success: function (res) {
        console.log("前台token存在，校验token");
        //如果后台缓存失效了
        if (res.data.TokenExpired) {
          console.log("后台缓存已经过期了,小程序清空token和userinfo和openid");
          wx.removeStorageSync("token");
          wx.removeStorageSync("userInfo");
          wx.reLaunch({
            url: '/pages/auth/auth',
           })
           wx.showToast({
            title: '授权失效',
          })
           return;
        } else {
          console.log("后台缓存中的信息存在,直接返回");
          var mycar = wx.getStorageSync('userInfo').mycar;
          if(mycar){
            wx.redirectTo({
              url: '/pages/my/mycar/mycar',
             })
             return;
          }
        }
      }
    });
   }
   },
  
  // 车牌号开始
  addProvience(e){
    if (this.data.activeIndex == 1 && !isNaN(Number(e.currentTarget.dataset.value))){
      return
    }
    
    this.data.choosedArr[this.data.activeIndex] = e.currentTarget.dataset.value
    if (this.data.activeIndex < 6 ) {
      this.data.activeIndex = parseInt(this.data.activeIndex) + 1
      // if (!this.data.choosedArr[parseInt(this.data.activeIndex) + 1]){
        
      // }
    } 
    this.setData({
      choosedArr: this.data.choosedArr,
      activeIndex: this.data.activeIndex
    })
    
    // console.log('选择的车牌号111111', this.data.choosedArr)
    console.log(this.data.choosedArr.join(''))
    
  },
  activeNum(e){
    console.log(e.currentTarget.dataset.index)
    this.data.activeIndex = e.currentTarget.dataset.index
    this.setData({
      activeIndex: this.data.activeIndex
    })
  },
  cancel(){
    // 线判断当前是否有选中操作的车牌号
    if(this.data.activeIndex == -1) return
    
    this.data.choosedArr[this.data.activeIndex] = ''
    if (this.data.activeIndex > 0) {
      this.data.activeIndex = parseInt(this.data.activeIndex) - 1
      // if (!this.data.choosedArr[parseInt(this.data.activeIndex) + 1]){

      // }
    }
    this.setData({
      choosedArr: this.data.choosedArr,
      activeIndex: this.data.activeIndex
    })
  },
  boardStop(){
    this.setData({
      activeIndex: -1
    })
  },
  wanChange(e){
    console.log(e)
    this.setData({
      wanUnitIndex: e.detail.value
    })
  },
  qianChange(e) {
    console.log(e)
    this.setData({
      qianUnitIndex: e.detail.value
    })
  },
  sure(){
    this.setData({
      carView: false,
      lichengView: true
    })
    let result = this.data.choosedArr.every((item) => {
      return item
    })
    if(result){
      console.log('this.data.choosedArr',this.data.choosedArr.join(''))
      // this.setData({
      //   carStr: this.data.choosedArr.join('')
      // })
    }

    console.log(this.data.choosedArr.join(''))
    var openId = wx.getStorageSync('userInfo').openId
    wx.request({
        url: 'http://localhost:8080/ipark/cars/add',
        data:{
          carNum:this.data.choosedArr.join(''),
          openId:openId
        },
        header: { 
          'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
         method: 'GET',
        success: (result) => {
           this.setData({
            parkmostList: result.data.data.parkcount
           })
           let userInfo = wx.getStorageSync('userInfo');
           var carNum = result.data.data.carNum;
           console.log(typeof(userInfo));
           userInfo['mycar']=carNum;
           wx.setStorageSync('userInfo', userInfo);
        },
      });
    
  },
  yes:function () {
    wx.navigateTo({
      url: '/pages/my/mycar/mycar',
    })
  },
  forget:function () {
    wx.navigateTo({
      url: '/pages/my/mycar/mycar',
    })
  },
  // 车牌号结束
 

})

